package com.ym.b2b.buyer.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.buyer.dao.EmCBuyerDao;
import com.ym.b2b.buyer.service.EmCBuyerService;
import com.ym.b2b.buyer.sqlbean.EmCBuyer;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.commons.util.CheckData;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.ym.b2b.user.sqlbean.EmCUser;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Service
public class EmCBuyerServiceImpl extends BaseService implements EmCBuyerService {

    @Autowired
    private EmCBuyerDao buyDao;

    @Autowired
    private EmCUserDAO  userdao;

    private Logger         dbLog = Logger.getLogger("db");
    @Override
    public Result searchPageEmCBuyer(Page<EmCBuyer> page, EmCBuyer emCBuyer) throws DataAccessException {
        Result result = new Result();

        try {
            /*
             * if (emCBuyer.getSearchWhere() != null && !emCBuyer.getSearchWhere().equals("")) { String isLaterOrNumber
             * = "[0-9a-zA-Z]*"; Pattern p1 = Pattern.compile(isLaterOrNumber); Matcher m1 =
             * p1.matcher(emCBuyer.getSearchWhere()); if (ZcStringUtil.isLetters(emCBuyer.getSearchWhere())) {
             * emCBuyer.setSpell(emCBuyer.getSearchWhere().toUpperCase()); } else { String regEx =
             * "[A-Z]{0,1}[0-9]{1,10}[\\-]{0,1}[0-9]{0,3}"; Pattern p = Pattern.compile(regEx); Matcher m =
             * p.matcher(emCBuyer.getSearchWhere()); if (m.matches()) {
             * emCBuyer.setBuyerSearchCode(emCBuyer.getSearchWhere()); } else if (m1.matches()) {
             * emCBuyer.setSpell(emCBuyer.getSearchWhere().toUpperCase()); } } }
             */
            String areaCode = emCBuyer.getAreaCode();
            if (areaCode != null && !"".equals(areaCode)) {
                CheckData cd = new CheckData();
                areaCode = cd.checkSearchAreaCode(areaCode);
                emCBuyer.setAreaCode(areaCode);
            }
            result.setResultPage(buyDao.searchPageEmCBuyer(page, emCBuyer));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;

    }

    @Override
    public Result showEmCBuyerDetail(EmCBuyer emCBuyer) throws DataAccessException {
        Result result = new Result();
        try {

            // 采购商详情
            emCBuyer = buyDao.showEmCBuyerDetail(emCBuyer);
            // result.setResult("获取采购商详情成功!");
            // 已添加用户
            EmCUser emCUser = new EmCUser();
            emCUser.setExtendCode(emCBuyer.getBuyerCode());
            emCUser.setUserType(2);
            List<EmCUser> userList = userdao.searchEmCUserList(emCUser);

            result.putData("emCBuyer", emCBuyer);
            result.setRows(userList);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "获取采购商详情失败!");
        }
        return result;
    }

    @Override
    public Result insertBuyer(EmCBuyer emCBuyer) throws DataAccessException {
        Result result = this.checkInputEmCBuyer(emCBuyer);
        if (!result.getCode().equals("200")) return result;
        try {
            buyDao.insertBuyer(emCBuyer);
            dbLog.info("INSERT-->EM_C_BUYER 采购商编码：" + emCBuyer.getBuyerCode() +"  各字段值："+JSON.toJSONString(emCBuyer));
            result.setResult("添加采购商成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加采购商失败!");
        }
        return result;
    }

    @Override
    public Result updateBuyer(EmCBuyer emCBuyer) throws DataAccessException {
        Result result = this.checkInputEmCBuyer(emCBuyer);
        if (!result.getCode().equals("200")) return result;
        try {
            buyDao.updateBuyer(emCBuyer);
            dbLog.info("UPDATE-->EM_C_BUYER 采购商编码：" + emCBuyer.getBuyerCode() +"  各字段值："+JSON.toJSONString(emCBuyer));
            result.setResult("更新采购商成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "更新采购商失败!");
        }
        return result;
    }

    @Override
    public Result deleteEmCBuyer(EmCBuyer emCBuyer) throws DataAccessException {
        Result result = new Result();
        try {
            buyDao.deleteEmCBuyer(emCBuyer);
            dbLog.info("DELETE-->EM_C_BUYER 采购商编码：" + emCBuyer.getBuyerCode());
            result.setResult("删除采购商成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /*
     * 批量审批
     */
    @Override
    public Result editMultipleEmCBuyersStatus(String buyerIdList) throws DataAccessException {
        Result result = new Result();
        try {
            buyDao.editMultipleEmCBuyersStatus(buyerIdList);
            dbLog.info("UPDATE(Multiple-审核通过)-->EM_C_BUYER 采购商编码：" + buyerIdList);
            result.setResult("批量审批采购商成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "批量审批采购商失败！");
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * @see com.ym.b2b.buyer.service.EmCBuyerService#deleteMultipleEmCBuyers(java.lang.String)
     */
    @Override
    public Result deleteMultipleEmCBuyers(String buyerIdList) throws DataAccessException {
        Result result = new Result();
        try {
            buyDao.deleteMultipleEmCBuyers(buyerIdList);
            dbLog.info("UPDATE(Multiple-删除)-->EM_C_BUYER 采购商编码：" + buyerIdList);
            result.setResult("批量删除供应商成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    private Result checkInputEmCBuyer(EmCBuyer p) {
        Result result = new Result();
        if (StringUtils.isBlank(p.getAreaCode())) {
            result.setCodeResult(Result.CODE_100, "缺少areaCode地区!");
            return result;
        }
        if (StringUtils.isBlank(p.getAddress())) {
            result.setCodeResult(Result.CODE_100, "缺少address地址!");
            return result;
        }
        if (StringUtils.isBlank(p.getZip())) {
            result.setCodeResult(Result.CODE_100, "缺少zip邮编!");
            return result;
        }
        if (StringUtils.isBlank(p.getPhone())) {
            result.setCodeResult(Result.CODE_100, "缺少phone收货联系电话!");
            return result;
        }

        p.setUpdateDate(new Date());
        return result;

    }
}
